package com.anlogic.sdk.workspace;

import java.io.File;
import org.apache.logging.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.anlogic.sdk.utils.EnvUtils;
import com.anlogic.sdk.utils.LogUtils;
import com.anlogic.sdk.utils.XmlUtils;

public class FDWorkspace implements IFDWorkspace {
	private static Logger logger = LogUtils.getLogger(FDWorkspace.class);
	
	public static final String PROPERTY_FILE_NAME = ".fdworkspace";
	
	private File propertyFile;
	
	private Document properties;
	
	public FDWorkspace() {
		String propertyPath = EnvUtils.getWorkspacePath() + "/" + PROPERTY_FILE_NAME;
		propertyFile = new File(propertyPath);
	}
	
	public boolean propertyFileExists() {
		return this.propertyFile.exists();
	}
	
	public void loadProperties() {
		try {
			logger.info("Load workspace property file...");
			properties = XmlUtils.parseFile(propertyFile);
		} catch (Exception e) {
			logger.error("Load workspace property file failed!");
		}
		logger.info("Load workspace property file done");
	}
	
	public void createNewPropertyFile() {
		if (!this.propertyFile.exists()) {
			try {
				logger.info("Create workspace property file...");
				this.propertyFile.createNewFile();
			} catch (Exception e) {
				logger.error("Create workspace property file failed!");
			}
		}
		try {
			properties = XmlUtils.newDocument();
			createNode("/", WORKSPACE_DESCRIPTION);
			createNode(getXpath(WORKSPACE_DESCRIPTION), IDE_VERSION, EnvUtils.getIDEVersion());
			createNode(getXpath(WORKSPACE_DESCRIPTION), IDE_BUILD_ID, EnvUtils.getIDEBuildId());
			savePropertyFile();
		} catch (Exception e) {
			logger.error("Create workspace property file failed!");
		}
		logger.info("Create workspace property file done");
	}
	
	public void savePropertyFile() {
		try {
			logger.info("Save workspace property file...");
			XmlUtils.saveFile(propertyFile, properties);
		} catch (Exception e) {
			logger.error("Save workspace property file failed!");
		}
		logger.info("Save workspace property file done");
	}
	
	public String getXpath(String... args) {
		String uri = "/";
		for(String arg : args) {
			uri = uri + "/" + arg;
        }
		return uri;
	}
	
	public Node getNode(String path) {
		if (properties == null)
			return null;
		Node node = null;
		try {
			node = XmlUtils.getNode(properties, path);
		} catch (Exception e) {
			logger.error(String.format("Get workspace property %s failed!", path));
		}
		return node;
	}
	
	public NodeList getNodes(String path) {
		if (properties == null)
			return null;
		NodeList nodes = null;
		try {
			nodes = XmlUtils.getNodes(properties, path);
		} catch (Exception e) {
			logger.error("Get workspace property {} failed!", path);
		}
		return nodes;
	}
	
	public void createNode(String parentPath, String name) {
		if (properties == null)
			return;
		try {
			XmlUtils.createNode(properties, parentPath, name);
		} catch (Exception e) {
			logger.error("Create workspace property {} failed!", name);
		}
	}
	
	public void createNode(String parentPath, String name, String value) {
		if (properties == null)
			return;
		try {
			XmlUtils.createNode(properties, parentPath, name, value);
		} catch (Exception e) {
			logger.error("Create workspace property {} = {} failed!", name, value);
		}
	}
	
	public void setNodeTextContent(String xpath, String value) {
		if (properties == null)
			return;
		try {
			XmlUtils.setNodeTextContent(properties, xpath, value);
		} catch (Exception e) {
			logger.error("Set workspace property {} = {} failed!", xpath, value);
		}
	}
	
	public String getIDEVersion() {
		Node node = getNode(getXpath(WORKSPACE_DESCRIPTION, IDE_VERSION));
		if (null == node) {
			return null;
		}
		return node.getTextContent();
	}
	
	public void setIDEVersion(String version) {
		setNodeTextContent(getXpath(WORKSPACE_DESCRIPTION, IDE_VERSION), version);
	}
	
	public String getBuildId() {
		Node node = getNode(getXpath(WORKSPACE_DESCRIPTION, IDE_BUILD_ID));
		if (null == node) {
			return null;
		}
		return node.getTextContent();
	}
	
	public void setBuildId(String id) {
		setNodeTextContent(getXpath(WORKSPACE_DESCRIPTION,  IDE_BUILD_ID), id);
	}
}
